package com.example.wangning.mycarofcountry.recommend.technology;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.example.mycarofcountry.entity.dao.Recommend_Technologylist;
import com.example.wangning.mycarofcountry.R;
import com.example.wangning.mycarofcountry.cache.VolleySingleton;

import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;

/**
 * Created by dllo on 16/1/14.
 */
public class TechnologyAdapter extends RecyclerView.Adapter {
    private VolleySingleton volleySingleton;
    private List<Recommend_Technologylist> data;
    private ImageLoader imageLoader;
    private Context context;

    public TechnologyAdapter(Context context) {
        this.context = context;
        volleySingleton = VolleySingleton.getVolleyIntance();
        imageLoader = volleySingleton.getImageLoader();
        data = new ArrayList<>();
    }


    public void addData(List<Recommend_Technologylist> datas){
        data = datas;
        notifyDataSetChanged();
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(context).inflate(R.layout.recycler_technology_main,parent,false);
            MainViewHolder mainViewHolder = new MainViewHolder(view);
            return mainViewHolder;
        }else {
            View view = LayoutInflater.from(context).inflate(R.layout.recycler_technology_other,parent,false);
            OtherViewHolder otherViewHolder = new OtherViewHolder(view);
            return otherViewHolder;
        }

    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (getItemViewType(position) == 0){
            ((MainViewHolder)holder).titleTv.setText(data.get(position).getTitle());
            ((MainViewHolder)holder).timeTv.setText(data.get(position).getTime());
            ((MainViewHolder)holder).typeTv.setText(data.get(position).getType());
            String imgUrl = data.get(position).getSmallpic();
            volleySingleton.getBitmap(volleySingleton, imgUrl, ((MainViewHolder)holder).imageView);

        }else{
            ((OtherViewHolder)holder).titleTv.setText(data.get(position).getTitle());
            ((OtherViewHolder)holder).timeTv.setText(data.get(position).getTime());
            ((OtherViewHolder)holder).typeTv.setText(data.get(position).getType());
            String imgUrl = data.get(position).getIndexdetail();
            String split = "㊣";
            StringTokenizer token = new StringTokenizer(imgUrl, split);
            ArrayList<String> strings = new ArrayList<>();
            while (token.hasMoreTokens()) {
                strings.add(token.nextToken());
            }
            for (int i = 0; i < strings.size(); i++) {
                String url_f = strings.get(0);
                String url_s = strings.get(1);
                String url_t = strings.get(2);
                volleySingleton.getBigBitmap(volleySingleton, url_f, ((OtherViewHolder) holder).imageView_f);
                volleySingleton.getBigBitmap(volleySingleton, url_s, ((OtherViewHolder) holder).imageView_s);
                volleySingleton.getBigBitmap(volleySingleton, url_t, ((OtherViewHolder)holder).imageView_t);
            }

        }

    }

    @Override
    public int getItemCount() {
        return data.size();
    }

    @Override
    public int getItemViewType(int position) {
        return data.get(position).getMediatype();
    }
    class MainViewHolder extends RecyclerView.ViewHolder{
        TextView titleTv,timeTv,typeTv;
        ImageView imageView;
        public MainViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            timeTv = (TextView) itemView.findViewById(R.id.timeTv);
            typeTv = (TextView) itemView.findViewById(R.id.typeTv);
            imageView = (ImageView) itemView.findViewById(R.id.imgIv);
        }
    }
    class OtherViewHolder extends RecyclerView.ViewHolder{
        TextView titleTv,timeTv,typeTv;
        ImageView imageView_f,imageView_s,imageView_t;
        public OtherViewHolder(View itemView) {
            super(itemView);
            titleTv = (TextView) itemView.findViewById(R.id.titleTv);
            timeTv = (TextView) itemView.findViewById(R.id.timeTv);
            typeTv = (TextView) itemView.findViewById(R.id.typeTv);
            imageView_f = (ImageView) itemView.findViewById(R.id.imageView_f);
            imageView_s = (ImageView) itemView.findViewById(R.id.imageView_s);
            imageView_t = (ImageView) itemView.findViewById(R.id.imageView_t);
        }
    }
}
